<?php

namespace App\Admin\Controllers;

use Dcat\Admin\Http\Controllers\AuthController as BaseAuthController;
use Illuminate\Support\Facades\Validator;

class AuthController extends BaseAuthController
{
    protected $view = 'admin.login';

    public function postLogin(\Illuminate\Http\Request $request)
    {
        $credentials = $request->only([$this->username(), 'password', 'captcha']);
        $remember = (bool) $request->input('remember', false);

        $messages = [
            'captcha.required' => '请输入验证码',
            'captcha.captcha'  => '验证码错误',
        ];

        $validator = Validator::make($credentials, [
            $this->username()   => 'required',
            'password'          => 'required',
            'captcha'           => 'required|captcha',
        ], $messages);

        if ($validator->fails()) {
            return $this->validationErrorsResponse($validator);
        }

        $credentials = $request->only([$this->username(), 'password']);

        if ($this->guard()->attempt($credentials, $remember)) {
            return $this->sendLoginResponse($request);
        }

        return $this->validationErrorsResponse([
            $this->username() => $this->getFailedLoginMessage(),
        ]);
    }
}
